package com.th


class ProjectController {

  def scaffold = true
  
    def index = { redirect(action:"display", id:"1") }
    
  def projectDisplay = {
    def project
    if(params.id) {
       project =  Project.findById(params.id)
    }else {
      project = Project.list();
      //redirect (view that displays all projects)
    }
    
    [project : project, tags:Tag.list()]
  }
  
  def createProject = {
    def project = new Project(params)
    if(project.validate()) {
      project.save()
      redirect(controller:'project', action:'projectDisplay', id:project.id)
    }else {
      flash.message = "Failed creating the project"
    }
    
  }
  
  def addTag = {
    def  project =  Project.findById(params.id)
    def tag = Tag.findByName(params.tagName)
    
    def technologyUsed = new TechnologyUsed(params).save()
    project.addToTechnologies(technologyUsed)
    tag.addToTechnologies(technologyUsed)
    redirect( action:'projectDisplay', id:project.id)
  }
  
  def displayAllProjects = {
    def projects = Project.list()
    [projects:projects]
  }
      
}